<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${daoModule.package}.${table.beanName}Dao">
    <resultMap id="BaseResultMap" type="${poModule.package}.${table.beanName}Bean">
        <id column="${table.pk.columnName}" jdbcType="${table.pk.columnType}" property="${table.pk.fieldName}" />
    <#list table.columns as col>
        <#if col.pk == false>
            <#if col.isJson()>
        <result column="${col.columnName}" typeHandler="com.cellulam.framework.datasource.dao.handlers.JsonTypeHandler" javaType="${col.javaTypeText}" property="${col.fieldName}" />
            <#else>
        <result column="${col.columnName}" ${col.jdbcTypeText} property="${col.fieldName}" />
            </#if>
        </#if>
    </#list>
    </resultMap>

    <resultMap type="com.cellulam.framework.datasource.FetchDataBean" id="fetchMap">
        <result property="pk" column="${table.pk.columnName}"/>
        <association property="bean" resultMap="BaseResultMap">
        </association>
    </resultMap>

    <sql id="BaseColumnList">
        <#list table.columns as col>`${col.columnName}`<#sep>, </#list>
    </sql>

    <sql id="BaseWhereClause">
        <where>
            <trim prefix="(" prefixOverrides="and" suffix=")">
            <#list table.columns as col>
                <#if col.hasCondition()>
                    <#if col.columnType == 'VARCHAR'>
                <if test="criteria != null and criteria.${col.fieldName} != null and criteria.${col.fieldName} != ''">
                    and `${col.columnName}` = ${"#"}{criteria.${col.fieldName}${col.jdbcTypeDefText}}
                </if>
                    <#else>
                <if test="criteria != null and criteria.${col.fieldName} != null">and `${col.columnName}` = ${"#"}{criteria.${col.fieldName}${col.jdbcTypeDefText}}
                </if>
                    </#if>
                </#if>
            </#list>
                <if test="conditions != null and conditions.size > 0">
                    <foreach collection="conditions" item="condition">
                        <if test="condition.effective">
                             <#list table.columns as col>
                            <choose>
                                <when test="condition.field == '${col.fieldName}'">
                                    and `${col.columnName}`
                                </when>
                            </choose>
                             </#list>
                            <if test="condition.operator == 1">
                                =
                            </if>
                            <if test="condition.operator == 2">
                                >
                            </if>
                            <if test="condition.operator == 3">
                                &lt;
                            </if>
                            <if test="condition.operator == 4">
                                >=
                            </if>
                            <if test="condition.operator == 5">
                                &lt;=
                            </if>
                            <if test="condition.operator == 6">
                                like CONCAT('%',${r'#{condition.value,jdbcType=VARCHAR}'},'%')
                            </if>
                            <if test="condition.operator == 7">
                                in
                                <foreach collection="condition.value" open="(" close=")" separator="," item="item" index="index">
                                    ${r'#{item}'}
                                </foreach>
                            </if>
                            <if test="condition.operator == 8">
                                not in
                                <foreach collection="condition.value" open="(" close=")" separator="," item="item" index="index">
                                ${r'#{item}'}
                                </foreach>
                            </if>
                            <if test="condition.operator == 9">
                                &lt;>
                            </if>
                            <if test="condition.operator != 6 and condition.operator != 7 and condition.operator != 8">
                                <if test="condition.jdbcType == 1">
                                    ${r'#{condition.value,jdbcType=VARCHAR}'}
                                </if>
                                <if test="condition.jdbcType == 2">
                                    ${r'#{condition.value,jdbcType=INTEGER}'}
                                </if>
                                <if test="condition.jdbcType == 3">
                                    ${r'#{condition.value,jdbcType=BIGINT}'}
                                </if>
                                <if test="condition.jdbcType == 4">
                                    ${r'#{condition.value,jdbcType=DATE}'}
                                </if>
                                <if test="condition.jdbcType == 5">
                                    ${r'#{condition.value,jdbcType=TIMESTAMP}'}
                                </if>
                            </if>
                        </if>
                    </foreach>
                </if>
            </trim>
<#if table.generateDel()>
            and is_del = 0
</#if>
        </where>
    </sql>

    <select id="getById" parameterType="${table.pk.fieldType.name}" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from ${table.sqlTableName}
        where ${table.pk.columnName} = ${"#"}{${table.pk.fieldName},jdbcType=${table.pk.columnType}}
    <#if table.generateDel()>
        and is_del = 0
    </#if>
    </select>

    <insert id="save" parameterType="${poModule.package}.${table.beanName}Bean"
            keyColumn="${table.pk.columnName}" keyProperty="${table.pk.fieldName}" useGeneratedKeys="true">
        insert into ${table.sqlTableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list table.columns as col>
                <#if col.hasCondition() && col.auto == false>

                    <#if col.columnType?lower_case == 'date' && col.columnName?lower_case == 'pdate'>
            `pdate`,
                    <#elseif col.columnType?lower_case == 'integer' && col.columnName?lower_case == 'update_user'>
            <if test="createUser != null">`update_user`,</if>
                    <#else>
            <if test="${col.fieldName} != null">`${col.columnName}`,</if>
                    </#if>
                </#if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list table.columns as col>
            <#if col.isJson()>
            <if test="${col.fieldName} != null">${"#"}{${col.fieldName}, typeHandler=com.cellulam.framework.datasource.dao.handlers.JsonTypeHandler, javaType=${col.javaTypeText}},</if>
            <#elseif col.hasCondition() && col.auto == false>
                <#if col.columnType?lower_case == 'date' && col.columnName?lower_case == 'pdate'>
            now(),
                <#elseif col.columnType?lower_case == 'integer' && col.columnName?lower_case == 'update_user'>
            <if test="createUser != null">${"#"}{createUser,jdbcType=INTEGER},</if>
                <#else>
            <if test="${col.fieldName} != null">${"#"}{${col.fieldName}${col.jdbcTypeDefText}},</if>
                </#if>
            </#if>
        </#list>
        </trim>
    </insert>

    <update id="update" parameterType="${poModule.package}.${table.beanName}Bean">
        update ${table.sqlTableName}
        <set>
        <#list table.columnsNoPk as col>
                <#if col.hasCondition()>
                    <#if col.isJson()>
            <if test="${col.fieldName} != null">`${col.columnName}` = ${"#"}{${col.fieldName}, typeHandler=com.cellulam.framework.datasource.dao.handlers.JsonTypeHandler, javaType=${col.javaTypeText}},</if>
                    <#elseif col.columnType?lower_case != 'date' || col.columnName?lower_case != 'pdate'>
                        <#if col.columnType?lower_case != 'integer' || col.columnName?lower_case != 'create_user'>
            <if test="${col.fieldName} != null">`${col.columnName}` = ${"#"}{${col.fieldName}${col.jdbcTypeDefText}},</if>
                        </#if>
                    </#if>
                </#if>
        </#list>
        </set>
        where ${table.pk.columnName} = ${"#"}{${table.pk.fieldName}${table.pk.jdbcTypeDefText}}
<#if table.generateDel()>
        and is_del = 0
</#if>
    </update>


    <select id="query" resultMap="BaseResultMap" parameterType="com.cellulam.framework.core.model.PagerCriteria" >
        select <include refid="BaseColumnList" />
        from ${table.sqlTableName}
        <include refid="BaseWhereClause" />
        <if test="sortFields != null and sortFields.size() > 0">
            order by
            <foreach collection="sortFields" item="item" separator=",">
                <choose>
                <#list table.columns as col>
                    <when test="item.field == '${col.fieldName}'">
                        ${col.columnName} <if test="item.sortType == 1">asc</if> <if test="item.sortType == 2">desc</if>
                    </when>
                </#list>
                </choose>
            </foreach>
        </if>
        limit ${"#"}{offset}, ${"#"}{limit}
    </select>

    <select id="count" parameterType="com.cellulam.framework.core.model.PagerCriteria" resultType="java.lang.Long">
        select count(1)
        from ${table.sqlTableName}
        <include refid="BaseWhereClause" />
    </select>

    <select id="fetch" parameterType="map" resultMap="fetchMap">
        select <include refid="BaseColumnList" />
        from ${table.sqlTableName}
        where ${table.pk.columnName} > ${"#"}{start}
            <#list table.columns as col>
                <#if col.hasCondition()>
                    <#if col.columnType == 'VARCHAR'>
                <if test="criteria != null and criteria.${col.fieldName} != null and criteria.${col.fieldName} != ''">
                    and `${col.columnName}` = ${"#"}{criteria.${col.fieldName}${col.jdbcTypeDefText}}
                </if>
                    <#else>
                <if test="criteria != null and criteria.${col.fieldName} != null">and `${col.columnName}` = ${"#"}{criteria.${col.fieldName}${col.jdbcTypeDefText}}
                </if>
                    </#if>
                </#if>
            </#list>
<#if table.generateDel()>
            and is_del = 0
</#if>
        limit ${"#"}{limit}

    </select>

<#if table.generateDel()>

    <update id="delete" parameterType="${table.pk.fieldType.name}">
        update ${table.sqlTableName}
        set is_del = ${"#"}{${table.pk.fieldName},jdbcType=${table.pk.columnType}}
        where ${table.pk.columnName} = ${"#"}{${table.pk.fieldName},jdbcType=${table.pk.columnType}}
        and is_del = 0
    </update>
</#if>


    <select id="lockById" parameterType="${table.pk.fieldType.name}" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from ${table.sqlTableName}
        where ${table.pk.columnName} = ${"#"}{${table.pk.fieldName},jdbcType=${table.pk.columnType}}
    <#if table.generateDel()>
        and is_del = 0
    </#if>
        for update
    </select>

<#if table.hasUniqKey()>

    <select id="lockByKey" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList" />
        from ${table.sqlTableName}
        <where>
            <trim prefix="(" prefixOverrides="and" suffix=")">
            <#list table.uniqKeyFields as col>
                    and `${col.columnName}` = ${"#"}{${col.fieldName}${col.jdbcTypeDefText}}
            </#list>
            </trim>
<#if table.generateDel()>
            and is_del = 0
</#if>
        </where>
        for update
    </select>

</#if>

</mapper>